package com.ysd.lis.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ysd.common.Result;
import com.ysd.lis.entity.TDictTreeItem;
import com.ysd.lis.entity.TDictTreeType;
import com.ysd.lis.mapper.TDictTreeItemMapper;
import com.ysd.lis.mapper.TDictTreeTypeMapper;
import com.ysd.lis.request.DictTreeItemAddParam;
import com.ysd.lis.request.DictTreeTypeAddParam;
import com.ysd.lis.service.TDictTreeService;
import com.ysd.lis.vo.TDictTreeItemVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * cold_chain.t_role 服务实现类
 * </p>
 *
 * @author Peng
 * @since 2021-10-08
 */
@Service
public class TDictTreeTypeServiceImpl extends ServiceImpl<TDictTreeTypeMapper, TDictTreeType> implements TDictTreeService {

    @Autowired
    TDictTreeTypeMapper tDictTreeTypeMapper;
    @Autowired
    TDictTreeItemMapper tDictTreeItemMapper;

    @Override
    public Result findDictReportFieldList(){
        Map returnMap = new HashMap<>();
        QueryWrapper<TDictTreeType> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("del_flag",0).orderByAsc("[order]");
        List<TDictTreeType> tDictTreeTypes = tDictTreeTypeMapper.selectList(queryWrapper);
        returnMap.put("fieldList",tDictTreeTypes);
        return Result.succ(1,"成功",returnMap) ;
    }

    @Override
    public Result findDictReportValueList(String typeId,Integer pageSize,Integer pageIndex){
        Map returnMap = new HashMap();
        Page<TDictTreeItem> page = new Page<>();
        QueryWrapper<TDictTreeItem> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("type_id",typeId).eq("del_flag",0).orderByAsc("[order]");
        List<TDictTreeItem> tDictTreeItems = tDictTreeItemMapper.selectList(queryWrapper);
        List<TDictTreeItem> collect = tDictTreeItems.stream().skip((pageIndex - 1) * pageSize).limit(pageSize).collect(Collectors.toList());
        List<TDictTreeItemVo> list = new ArrayList<>();
        collect.forEach(a->{
            TDictTreeItemVo tDictTreeItemVo = new TDictTreeItemVo();
            BeanUtil.copyProperties(a,tDictTreeItemVo);
            tDictTreeItemVo.setCreateTime(DateUtil.format(a.getCreateTime(),"yyyy-MM-dd HH:mm:ss"));
            list.add(tDictTreeItemVo);
        });
        returnMap.put("itemList",list);
        returnMap.put("count",tDictTreeItems.size());
        return Result.succ(1,"查询成功",returnMap);
    }

    @Override
    public Result addDictReportField(DictTreeTypeAddParam dictTreeTypeAddParam){
        LambdaQueryWrapper<TDictTreeType> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(TDictTreeType::getCode,dictTreeTypeAddParam.getCode()).eq(TDictTreeType::getDelFlag,0);
        List<TDictTreeType> tDictTreeTypes = tDictTreeTypeMapper.selectList(lambdaQueryWrapper);
        if(CollectionUtils.isNotEmpty(tDictTreeTypes)){
            return Result.fail(400,"编码不能重复！","");
        }
        Map returnMap = new HashMap();
        TDictTreeType tDictTreeType  = new TDictTreeType();
        BeanUtil.copyProperties(dictTreeTypeAddParam,tDictTreeType);
        int insert = tDictTreeTypeMapper.insert(tDictTreeType);
        if(insert>0){
            returnMap.put("typeId",tDictTreeType.getId());
            return Result.succ(1,"添加成功",returnMap);
        }
        return null;
    }
    @Override
    public Result editDictReportField(DictTreeTypeAddParam dictTreeTypeAddParam){
        Map returnMap = new HashMap();
        TDictTreeType tDictTreeType  = new TDictTreeType();
        BeanUtil.copyProperties(dictTreeTypeAddParam,tDictTreeType);
        int update = tDictTreeTypeMapper.updateById(tDictTreeType);
        if(update>0){
            returnMap.put("typeId",tDictTreeType.getId());
            return Result.succ(1,"修改成功",returnMap);
        }
        return null;
    }

    @Override
    public Result deleteDictReportField(String id){
        int deleteById = tDictTreeTypeMapper.deleteById(id);
        if(deleteById>0){
            LambdaQueryWrapper<TDictTreeItem> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(TDictTreeItem::getTypeId,id);
            tDictTreeItemMapper.delete(lambdaQueryWrapper);
        }



        //tDictTreeItemMapper.update(tDictTreeItem,lambdaQueryWrapper);

        return Result.succ(1,"删除成功","");
    }
    @Override
    public Result addDictReportValue(DictTreeItemAddParam dictTreeItemAddParam){

        LambdaQueryWrapper<TDictTreeItem> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(TDictTreeItem::getTypeId,dictTreeItemAddParam.getClassId()).eq(TDictTreeItem::getCode,dictTreeItemAddParam.getCode()).eq(TDictTreeItem::getDelFlag,0);
        List<TDictTreeItem> tDictTreeItems = tDictTreeItemMapper.selectList(lambdaQueryWrapper);
        if(CollectionUtils.isNotEmpty(tDictTreeItems)){
            return Result.fail(400,"编码不能重复！","");
        }

        TDictTreeItem tDictTreeItem = new TDictTreeItem();
        BeanUtil.copyProperties(dictTreeItemAddParam,tDictTreeItem);
        tDictTreeItemMapper.insert(tDictTreeItem);
        return Result.succ(1,"添加成功","");
    }
    @Override
    public Result editDictReportValue(DictTreeItemAddParam dictTreeItemAddParam){
        TDictTreeItem tDictTreeItem = new TDictTreeItem();
        BeanUtil.copyProperties(dictTreeItemAddParam,tDictTreeItem);
        tDictTreeItemMapper.updateById(tDictTreeItem);
        return Result.succ(1,"修改成功","");
    }

    @Override
    public Result deleteDictReportValue(String id){
//        TDictTreeItem tDictTreeItem = new TDictTreeItem();
//        tDictTreeItem.setDelFlag(1);
//        tDictTreeItem.setId(id);
//        tDictTreeItemMapper.updateById(tDictTreeItem);
        tDictTreeItemMapper.deleteById(id);
        return Result.succ(1,"删除成功","");
    }

    @Override
    public Result getDictValueByType(String code){
        LambdaQueryWrapper<TDictTreeType> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(TDictTreeType::getDelFlag,0).eq(TDictTreeType::getCode,code);

        TDictTreeType tDictTreeType = tDictTreeTypeMapper.selectOne(lambdaQueryWrapper);
        if(tDictTreeType!=null&&tDictTreeType.getId()!=null){
            LambdaQueryWrapper<TDictTreeItem> lambdaQueryWrapper1 = new LambdaQueryWrapper<>();
            lambdaQueryWrapper1.eq(TDictTreeItem::getDelFlag,0).eq(TDictTreeItem::getTypeId,tDictTreeType.getId());
            List<TDictTreeItem> tDictTreeItems = tDictTreeItemMapper.selectList(lambdaQueryWrapper1);
            if(CollectionUtils.isNotEmpty(tDictTreeItems)){
                return Result.succ(1,"查询成功",tDictTreeItems);
            }
        }
        return null;
    }

    @Override
    public Result getDictValueByTypes(String code) {
        LambdaQueryWrapper<TDictTreeType> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        String[] codes = code.split(",");
        lambdaQueryWrapper.eq(TDictTreeType::getDelFlag,0).in(TDictTreeType::getCode,codes);
        List<TDictTreeType> tDictTreeTypes = tDictTreeTypeMapper.selectList(lambdaQueryWrapper);
        if(tDictTreeTypes.size()>0){
            List<String> typeIds = tDictTreeTypes.stream().map(TDictTreeType::getId).collect(Collectors.toList());
            LambdaQueryWrapper<TDictTreeItem> lambdaQueryWrapper1 = new LambdaQueryWrapper<>();
            lambdaQueryWrapper1.eq(TDictTreeItem::getDelFlag,0).eq(TDictTreeItem::getStatus,1).in(TDictTreeItem::getTypeId,typeIds);
            List<TDictTreeItem> tDictTreeItems = tDictTreeItemMapper.selectList(lambdaQueryWrapper1);

            for(TDictTreeItem item : tDictTreeItems){
                int indexOf = typeIds.indexOf(item.getTypeId());
                if(indexOf>=0){
                    TDictTreeType tDictTreeType = tDictTreeTypes.get(indexOf);
                    List<TDictTreeItem> itemList = tDictTreeType.getItemList();
                    if(null == itemList){
                        itemList = new ArrayList<>();
                        tDictTreeType.setItemList(itemList);
                    }
                    itemList.add(item);

                }
            }

        }

        return Result.succ(1,"查询成功",tDictTreeTypes);
    }

    @Override
    public Result getDictTreeTypeByOrgId(String OrgId) {
        LambdaQueryWrapper<TDictTreeType> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TDictTreeType::getDelFlag,0);
        if(StringUtils.isNotBlank(OrgId)){
            queryWrapper.eq(TDictTreeType::getOrgId,OrgId);
        }
        queryWrapper.orderByAsc(TDictTreeType::getOrder);
        List<TDictTreeType> typeList = tDictTreeTypeMapper.selectList(queryWrapper);
        //获取树
        List<Map> typeTree = new ArrayList<>();
        for(TDictTreeType type : typeList){
            if(StringUtils.isBlank(type.getPid())){
                Map map = new HashMap();
                map.put("id",type.getId());
                map.put("name",type.getName());
                map.put("code",type.getCode());
                map.put("orgId",type.getOrgId());
                map.put("order",type.getOrder());
                map.put("pid",type.getPid());
                typeTree.add(map);
            }
        }
        for(Map m : typeTree){
            List<Map> child = getChild(m,typeList);
            m.put("children",child);
        }
        return Result.succ(1,"查询成功",typeTree);
    }

    private List<Map> getChild(Map m, List<TDictTreeType> typeList) {
        List<Map> result = new ArrayList();
        for(TDictTreeType type : typeList){
            if(StringUtils.isNotBlank(type.getPid())&&type.getPid().equals(m.get(("id")))){
                Map map = new HashMap();
                map.put("id",type.getId());
                map.put("name",type.getName());
                map.put("code",type.getCode());
                map.put("orgId",type.getOrgId());
                map.put("order",type.getOrder());
                map.put("pid",type.getPid());
                result.add(map);
            }
        }
        for(Map ma : result){
            ma.put("children",getChild(ma,typeList));
        }
        if (result.size() == 0) {
            return null;
        }
        return result;
    }
}
